﻿using System;
using System.Windows.Forms;
using CuteSprite.Eme.Data.Settlement;
using CuteSprite.Eme.UI.Utils;
using HmFramework.Components;
using HmFramework.UI.Utils;

namespace CuteSprite.Eme.UI.Settlement
{
	public partial class FrmBsdSettlementSolutionAdmin : EmeCommonAdminForm
	{
		#region -- 属性 --

		#endregion

		#region -- 构造 --

		/// <summary>默认构造函数</summary>
		public FrmBsdSettlementSolutionAdmin()
		{
			EntityDataSource = BsdEntityHelper.ModelSettlement;
			EntityDataTable = BsdEntityHelper.SSTableSettlementSolution;

			//this.ShowDialog(this);
			InitForm();
		}

		#endregion

		#region -- 控件初始化 --

		private ButtonX btnBillingSolutionManagement;

		protected override void InitComponent()
		{
			base.InitComponent();

			btnBillingSolutionManagement = CreateButton("btnBillingSolutionManagement", "管理计费方案", 8);
			btnBillingSolutionManagement.Location = new System.Drawing.Point(431, 7);
			btnBillingSolutionManagement.Size = new System.Drawing.Size(98, 23);
			btnBillingSolutionManagement.Click += new EventHandler(btnBillingSolutionManagement_Click);

			ToolbarPanelControl.Controls.Add(btnBillingSolutionManagement);
		}

		#endregion

		#region -- 权限项匹配 --

		/// <summary>已重载，操作权限项匹配</summary>
		protected override void MatchPermissionItems()
		{
			base.MatchPermissionItems();

			btnBillingSolutionManagement.Visible = ServerManagement.IsAuthorized(PermissionItemSetsCode, "BillingSolutionManagement") && CurrentModelViewIsEffective;
		}

		#endregion

		#region -- 删除 --

		protected override void OnDeleteButtonClick()
		{
			var list = SelectedEntities;
			if (list != null)
			{
				foreach (var ientity in list)
				{
					//
					if (((String)ientity[SettlementSolution.__.Code]).EqualIgnoreCase("DS"))
					{
						DialogHelper.ShowWarning(null, "默认公布价方案不允许被删除！");
						return;
					}
				}
			}

			base.OnDeleteButtonClick();
		}

		#endregion

		#region -- 管理计费方案 --

		private void btnBillingSolutionManagement_Click(object sender, EventArgs e)
		{
			var selSolution = SelectedEntity as SettlementSolution;
			if (selSolution == null)
			{
				MessageBoxEx.Show("请选择需要管理的公布价方案！", "公布价方案", MessageBoxButtons.OK, MessageBoxIcon.Warning);
			}
			else
			{
				// 判断公布价方案代码是否为空，如果为空从服务器取代码
				if (selSolution.Code.IsNullOrWhiteSpace())
				{
					lock (ServerManagement.LockSynchronizer)
					{
						var settlementslolution = ServerManagement.LoadEntityByKey(SettlementSolution.Meta.Factory, BsdEntityHelper.ModelSettlement, BsdEntityHelper.SSTableSettlementSolution, selSolution.ID) as SettlementSolution;
						var frm = new FrmBillingSolutionAdmin(settlementslolution.ID, settlementslolution.Code);
						var result = frm.ShowDialog(this);
						frm.Close();
					}
				}
				else
				{
					var frm = new FrmBillingSolutionAdmin(selSolution.ID, selSolution.Code);
					var result = frm.ShowDialog(this);
					frm.Close();
				}
			}
		}

		#endregion
	}
}